<?php
namespace common\modules\user\models;

use common\modules\storage\behaviors\UploadBehavior;
use Yii;

/**
 * This is the model class for table "user_profile".
 *
 * @property integer $user_id
 * @property integer $group_id
 * @property string $realname
 * @property string $nickname
 * @property integer $gender
 * @property string $picture
 * @property string $avatar
 * @property string $avatar_path
 * @property string $avatar_url
 * @property integer $fans_count
 * @property integer $follow_count
 * @property integer $favorite_count
 * @property integer $visited_count
 * @property integer $locale
 * @property User $user
 * @property UserGroup $userGroup
 */
class UserProfile extends \yii\db\ActiveRecord
{

    const GENDER_MALE = 1;

    const GENDER_FEMALE = 2;

    public $picture;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user_profile}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'picture' => [
                'class' => UploadBehavior::className(),
                'attribute' => 'picture',
                'pathAttribute' => 'avatar_path',
                'urlAttribute' => 'avatar_url'
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'user_id'
                ],
                'required'
            ],
            [
                [
                    'user_id',
                    'gender'
                ],
                'integer'
            ],
            [
                [
                    'gender'
                ],
                'in',
                'range' => [
                    self::GENDER_FEMALE,
                    self::GENDER_MALE
                ]
            ],
            [
                [
                    'nickname',
                    'realname'
                ],
                'string',
                'max' => 20
            ],
            [
                [
                    'avatar_path',
                    'avatar_url'
                ],
                'string',
                'max' => 255
            ],
            [
                'locale',
                'default',
                'value' => Yii::$app->language
            ],
            [
                'locale',
                'in',
                'range' => array_keys(Yii::$app->params['availableLocales'])
            ],
            [
                'picture',
                'safe'
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'user_id' => Yii::t('common', 'User ID'),
            'nickname' => Yii::t('common', 'Nickname'),
            'realname' => Yii::t('common', 'Realname'),
            'locale' => Yii::t('common', 'Locale'),
            'picture' => Yii::t('common', 'Picture'),
            'gender' => Yii::t('common', 'Gender'),
            'fans_count' => Yii::t('common', 'Fans Count'),
            'follow_count' => Yii::t('common', 'Follow Count'),
            'favorite_count' => Yii::t('common', 'Favorite Count'),
            'visited_count' => Yii::t('common', 'Visited Count')
        ];
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        Yii::$app->session->setFlash('forceUpdateLocale');
    }

    /**
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), [
            'id' => 'user_id'
        ]);
    }

    /**
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUserGroup()
    {
        return $this->hasOne(UserGroup::className(), [
            'id' => 'group_id'
        ]);
    }

    public function getFullName()
    {
        if ($this->nickname) {
            return $this->nickname;
        }
        if ($this->realname) {
            return $this->realname;
        }
        return null;
    }

    public function getAvatar($default = null)
    {
        return $this->avatar_url ? Yii::getAlias($this->avatar_url) : $default;
    }
}
